package app.kernel.connector.config;

import java.net.URL;
import java.util.Hashtable;

import kernel.udp.invoke.UDPClient;
import kernel.udp.invoke.XmlRpcClientConfigImpl;



public class XmlRpcOutgoingConnection {

    public final String CSM_ADDRESS = config.PSG_Configuration.CSM_Address;

	// XMLRPC Client Configurations
	public static XmlRpcClientConfigImpl CSM_Configuration;

	public static Hashtable CSG_Configurations = new Hashtable();
	
	public static Hashtable PSG_Configurations = new Hashtable();

	public static Hashtable CP_Configurations = new Hashtable();

	public static Hashtable SC_Gen_Configurations = new Hashtable();
	
	public static Hashtable CSG_Gen_Configurations = new Hashtable();
	
	public XmlRpcOutgoingConnection() throws Exception{
		
		// Setup HTTP Connection
		CSM_Configuration = new XmlRpcClientConfigImpl(CSM_ADDRESS);
		
	}
	
	public UDPClient getCSM_Client(){
		
        UDPClient client = new UDPClient(CSM_Configuration);
		return client;
		
	}
	
	// Get CSG Client
	public UDPClient getCSG_Client(String CSG_Name){
		
		if (CSG_Configurations.containsKey(CSG_Name)){
			
	        UDPClient client = new UDPClient((XmlRpcClientConfigImpl)CSG_Configurations.get(CSG_Name));
	        return client;
	        
		}
		
		else
			
		  return null;
		
	}

	// Get SC_Gen_Client 
	public UDPClient getSC_Gen_Client(String CSG_Name){
		
		if (SC_Gen_Configurations.containsKey(CSG_Name)){
			
	        UDPClient client = new UDPClient((XmlRpcClientConfigImpl)SC_Gen_Configurations.get(CSG_Name));
	        return client;
	        
		}
		
		else
			
		  return null;
		
	}

	// Get SC_Gen_Client 
	public UDPClient getCSG_Gen_Client(String ID){
		
		if (CSG_Gen_Configurations.containsKey(ID)){
			
	        UDPClient client = new UDPClient((XmlRpcClientConfigImpl)CSG_Gen_Configurations.get(ID));
	        return client;
	        
		}
		
		else
			
		  return null;
		
	}

	// Get PSG Client
	public UDPClient getPSG_Client(String PSG_Address){
		
		if (PSG_Configurations.containsKey(PSG_Address)){
			
	        UDPClient client = new UDPClient((XmlRpcClientConfigImpl)PSG_Configurations.get(PSG_Address));
	        return client;
		}
		
		else
			
		  return null;
		
	}

	// Get CP Client
	public UDPClient getCP_Client(String CP_Name){
		
		if (CP_Configurations.containsKey(CP_Name)){
			
	        UDPClient client = new UDPClient((XmlRpcClientConfigImpl)CP_Configurations.get(CP_Name));
	        return client;
		}
		
		else
			
		  return null;
		
	}
	
	
	// Setup HTTP Connection
	public void addCSG_Configuration(String CSG_Name, String CSG_Address) throws Exception{       

		if (!CSG_Configurations.containsKey(CSG_Name)){
	        

	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(CSG_Address);	        
	        CSG_Configurations.put(CSG_Name, config);
	        
			
		}
		else {
			System.out.println("END");
			
		}
	
		
	}

	
	
	// Setup HTTP Connection
	public void addCSG_Gen_Configuration(String CSG_Name, String CSG_Address) throws Exception{       

		if (!CSG_Gen_Configurations.containsKey(CSG_Name)){
	        
			

	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(CSG_Address);	        
			CSG_Gen_Configurations.put(CSG_Name, config);
	        
			
		}
		else {
			System.out.println("END");
			
		}
	
		
	}
	
	
	// Setup HTTP Connection
	public void addSC_Gen_Configuration(String CSG_Name, String CSG_Address) throws Exception{       

		if (!SC_Gen_Configurations.containsKey(CSG_Name)){
	        
			

	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(CSG_Address);	        
			SC_Gen_Configurations.put(CSG_Name, config);
	        
			
		}
		else {
			System.out.println("END");
			
		}
	
		
	}
	
	// Setup HTTP Connection
	public void addPSG_Configuration(String PSG_Name, String PSG_Address) throws Exception{
		
		if (!PSG_Configurations.containsKey(PSG_Name)){

	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(PSG_Address);
			
			PSG_Configurations.put(PSG_Address, config);
			//System.out.println("Check Adding: "+PSG_Configurations.get(PSG_Address));

		}
		else{
			//System.out.println("Check Adding: "+PSG_Configurations.get(PSG_Address));
			
		}
		
	}
	
	// Setup HTTP Connection with Coordinator Peer
	public void addCP_Configuration(String CP_Name, String CP_Address) throws Exception{
		
		if (!CP_Configurations.containsKey(CP_Name)){

	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(CP_Address);
			CP_Configurations.put(CP_Name, config);
			
		}
		else{
			
			System.out.println("been here");
	        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(CP_Address);
			CP_Configurations.remove(CP_Name);
			CP_Configurations.put(CP_Name, config);
			
			
		}
		
	}
	

	
	public XmlRpcClientConfigImpl getCSG_Gen_Configuration(String ID){
		
		
		return (XmlRpcClientConfigImpl) CSG_Gen_Configurations.get(ID);
		
		
	}

	public XmlRpcClientConfigImpl getSC_Gen_Configuration(String ID){
		
		
		return (XmlRpcClientConfigImpl) SC_Gen_Configurations.get(ID);
		
		
	}
	
	public XmlRpcClientConfigImpl getCSG_Configuration(String CSG_Name){
		
		
		return (XmlRpcClientConfigImpl) CSG_Configurations.get(CSG_Name);
		
		
	}
	
	public XmlRpcClientConfigImpl getCSM_Configuration(){
		
		
		return CSM_Configuration;
		
	}
	
	public boolean existsCP(String CSG_Name, String SCName){
		
		return CP_Configurations.containsKey(CSG_Name+"@"+SCName);
		
	}
	
	public boolean existsPSG(String PSG_Address){
		
		return PSG_Configurations.containsKey(PSG_Address);
		
	}
	
	public boolean existsSC_Gen(String ID){
		
		return SC_Gen_Configurations.containsKey(ID);
		
	}
	public boolean existsCSG_Gen(String ID){
		
		return CSG_Gen_Configurations.containsKey(ID);
		
	}
	
	
}
